<?php
if(!defined('SYSTEM_PATH' ) ) die('Cannot access this page directly or see your configuration file ' ) ;
/**
 * Model
 * 
 * @package Cosmo
 * @author mentiss
 * 
 * @version 1.0
 * @access public
 */
class Model extends Cosmo {
	public 	$db ;
	
	/**
	 * Model::__construct()
	 * 
	 * @param mixed $loader
	 */
	final public function __construct() {
		parent::__construct() ;
		$this->db =& $this->load->database('default') ; // load default database 
		
		if(!isset($this->name) || is_null($this->name)) { // define table name to use if not defined
			$this->name = strtolower(str_replace('Model', '', get_class($this))) ;
		}
		
		// init field name and field type
		if(!isset($this->fields)) {
			$this->fields = $this->db->getFields($this->name) ;
		}
		$this->_init() ; // launch method to init all user want to init		
	}
	/**
	 * Model::_init()
	 * 
	 * @desc Need to be redifine in child if need to initiate some speical things
	 * 
	 * @return void 
	 */
	public function _init() {
	}
	/**
	 * Model::__call()
	 * 
	 * @desc Allow to make allias to selection in table
	 * 
	 * @param string $method
	 * @param mixed $params
	 * 
	 * @return mixed
	 */
	public function __call($method,$params) {
		$sql = null ;
		if(ereg('findBy', $method)) { // User call to find table entry by a field
			$field = substr($method,6) ;
			if(!ereg('And',$field)) {
				if(isset($this->fields[$field])) {
					if(false !== ($var = $this->_checkFieldType($this->fields[$field]['type'], $params[0]))) {
						// set sql query
						$sql = "SELECT * FROM ".$this->name." WHERE ".$this->fields[$field]['name']." = ".$var ;
					} 
				}				
			} else {
				// many field to select explode them
				$arrayField = explode('And',$field) ;
				$nbField = count($arrayField) ;
				$conditions = '' ;
				$i = 0 ;
				if(is_array($params) && count($params) == $nbField) {
					foreach($arrayField as $oneField) { // create conditions
						if(isset($this->fields[$oneField]) && false !== ($var = $this->_checkFieldType($this->fields[$oneField]['type'],$params[$i]))) {
							$conditions .= $this->fields[$oneField]['name']."=".$var ; 
							if($i < $nbField)
								$conditions .= ' AND' ;
						}
						$i++ ;
					}
				}
				
				if($conditions != '') {
					// Set sql query
					$sql = "SELECT * FROM " . $this->name . " ".$condition ;
				}				
			}		
		}
		
		if($method == 'findAll') { // select all entry in the table
			$sql = "SELECT * FROM ".$this->name ;
		}
		
		if(!is_null($sql)) { // check if there is an sql command or not
			$query = $this->db->query($sql) ; // execute commande
			if($query->getNumberOfRows() > 0) // check if there are results
				return $query->getResultByArray();
			$this->db->free_result() ;
		}
		
		return false;
	}
	/**
	 * Model::_checkFieldType()
	 * 
	 * @desc Check if the var has correct type and format it for sql query, either return false 
	 * 
	 * @param string $type
	 * @param mixed $var
	 * 
	 * @return mixed
	 */
	private function _checkFieldType($type, $var) {
		switch(strtolower($type)) {
			case 'string':
				if(is_string($var)) {					
					return "'".$var."'";
				}
				break ;
			case 'int':
				if(is_int($var))
					return $var;
				break ;
			case 'real': 
				if(is_real($var))
					return $var;
				break ;
			default :
				return "'".$var."'";
		}
		return false;
	} 
}
?>